#!/usr/bin/env ruby

require 'json'
require 'pathname'
require 'psych'

workflows_path = Pathname.new(__dir__).join('..', '.github', 'workflows')

workflows = Psych.load_file("workflows.yml", aliases: true)
workflows['workflows'].each do |k, v|
  # Roundtrip through JSON to perform a deep clone of the value.
  # If we don't, then anchors are preserved from the input file
  # and GitHub disallows anchors.
  v = JSON.parse(JSON.generate(v))

  workflow_path = workflows_path.join(k).sub_ext('.yml')
  File.open(workflow_path, 'w') do |file|
    file.write <<~EOF
      # This file was generated by ci/generate and should not be modified by hand
    EOF
    file.write(Psych.dump(v, line_width: -1))
  end
end
